﻿using Downloader.Domain;
using Downloader.Framework.Queries;
using Downloader.Infrastructure;
using Downloader.Web.Controllers.ViewModels;
using SharpArch.NHibernate;
using System.Collections.Generic;

namespace Downloader.Web.Controllers.Queries
{
    public class ArticleContentQuery : NHibernateQuery, IArticleContentQuery
    {
        public RecordList<ViewModels.ArticleContentViewModel> GetList(int page, int size)
        {
            var query = Session.QueryOver<ArticleContent>()
                .JoinQueryOver(c => c.Category)
                .OrderBy(x => x.CreateTime).Asc;
            var count = query.ToRowCountQuery();
            var totalCount = count.FutureValue<int>();
            var firstResult = (page - 1) * size;
            var recordList = new RecordList<ArticleContentViewModel>();
            recordList.Data = new List<ArticleContentViewModel>();
            var model = query.Skip(firstResult).Take(size).List();
            ArticleContentViewModel temp = null;
            foreach (var m in model)
            {
                temp = new ArticleContentViewModel(m);
                recordList.Data.Add(temp);
            }
            recordList.PageIndex = page;
            recordList.PageSize = size;
            recordList.RecordCount = totalCount.Value;
            return recordList;
        }

        public RecordList<ArticleContentViewModel> GetList(int categoryId, int page, int size)
        {
            var query = Session.QueryOver<ArticleContent>().Where(c => c.Category.Id == categoryId).OrderBy(x => x.CreateTime).Asc;
            var count = query.ToRowCountQuery();
            var totalCount = count.FutureValue<int>();

            var firstResult = (page - 1) * size;
            var recordList = new RecordList<ArticleContentViewModel>();
            recordList.Data = new List<ArticleContentViewModel>();
            var model = query.Skip(firstResult).Take(size).Future();
            ArticleContentViewModel temp = null;
            foreach (var m in model)
            {
                temp = new ArticleContentViewModel(m);
                recordList.Data.Add(temp);
            }
            recordList.PageIndex = page;
            recordList.PageSize = size;
            recordList.RecordCount = totalCount.Value;
            return recordList;
        }

        public RecordList<ArticleImgViewModel> GetImgList(int categoryId, int page, int size)
        {
            var query = Session.QueryOver<ArticleContent>().Where(c => c.Category.Id == categoryId).OrderBy(x => x.CreateTime).Asc;
            var count = query.ToRowCountQuery();
            var totalCount = count.FutureValue<int>();

            var firstResult = (page - 1) * size;
            var recordList = new RecordList<ArticleImgViewModel>();
            recordList.Data = new List<ArticleImgViewModel>();
            var model = query.Skip(firstResult).Take(size).Future();
            ArticleImgViewModel temp = null;
            foreach (var m in model)
            {
                temp = new ArticleImgViewModel(m);
                recordList.Data.Add(temp);
            }
            recordList.PageIndex = page;
            recordList.PageSize = size;
            recordList.RecordCount = totalCount.Value;
            return recordList;
        }


        public RecordList<ArticleContentViewModel> GetTopNumber(int categoryId, int topNumber)
        {
            var query = Session.QueryOver<ArticleContent>().OrderBy(x => x.CreateTime).Desc;
            var recordList = new RecordList<ArticleContentViewModel>();
            var model = query.Where(c => c.Category.Id == categoryId).Take(topNumber).Future();
            ArticleContentViewModel temp = null;
            recordList.Data = new List<ArticleContentViewModel>();
            foreach (var m in model)
            {
                temp = new ArticleContentViewModel(m);
                recordList.Data.Add(temp);
            }
            return recordList;
        }

        public RecordList<ArticleImgViewModel> GetTopNumberImg(int categoryId, int topNumber)
        {
            var query = Session.QueryOver<ArticleContent>().OrderBy(x => x.CreateTime).Desc;
            var recordList = new RecordList<ArticleImgViewModel>();
            var model = query.Where(c => c.Category.Id == categoryId).Take(topNumber).Future();
            ArticleImgViewModel temp = null;
            recordList.Data = new List<ArticleImgViewModel>();
            foreach (var m in model)
            {
                temp = new ArticleImgViewModel(m);
                recordList.Data.Add(temp);
            }
            return recordList;
        }
        /// <summary>
        /// 获取系统中所有的单页（关于、联系方式等一此单页内容的维护）;
        /// </summary>
        /// <returns></returns>
        public RecordList<ArticleContentViewModel> GetSingleList()
        {
            var query =
                Session.QueryOver<ArticleContent>()
                .JoinQueryOver(c => c.Category)
                       .OrderBy(x => x.CreateTime)
                       .Desc;
            var recordList = new RecordList<ArticleContentViewModel>();
            var model = query.Future();
            ArticleContentViewModel temp = null;
            recordList.Data = new List<ArticleContentViewModel>();
            foreach (var m in model)
            {
                temp = new ArticleContentViewModel(m);
                recordList.Data.Add(temp);
            }
            return recordList;
        }


    }
}